package chpt10;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class EventDemo extends JFrame {
    private JTextField textField;
    private JPasswordField passwordField;
    private JButton button;

    public EventDemo() {
        setTitle("事件演示");
        setSize(300, 200);
        // 点击窗体的关闭按钮时，什么也不做
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        textField = new JTextField("输入用户名", 10);
        textField.setBackground(Color.lightGray);
        passwordField = new JPasswordField(10);
        button = new JButton("显示");

        textField.addActionListener(new ActionListener() {
            // 在textField中按回车的时候触发
            public void actionPerformed(ActionEvent e) {
                String text = textField.getText();
                System.out.println("textField：按回车，输入了 " + text);
            }
        });
        // 为textField添加焦点事件监听器
        textField.addFocusListener(new FocusAdapter() {
        	// 获得焦点时，清空文本框并置底色为白色
        	public void focusGained(FocusEvent e) {
        		textField.setText(""); // 清空文本框
        		textField.setBackground(Color.WHITE);
        		System.out.println("textField: 得到焦点");
            }
        	// 失去焦点时显示文本框中的内容
            public void focusLost(FocusEvent e) {
                System.out.println("textField：失去焦点，用户名 "
                			+textField.getText());
            }
        });
        
        passwordField.addKeyListener(new KeyAdapter() {
        	// 按下按键时触发
            public void keyPressed(KeyEvent e) {
            	char keyChar = e.getKeyChar();
                System.out.println("passwordField：按下了按键 " + keyChar);
            }
        });

        button.addActionListener(new ActionListener() {
        	// 按下按钮时触发
            public void actionPerformed(ActionEvent e) {
                String text = textField.getText();
                String password = new String(passwordField.getPassword());
                System.out.print("button: 被按下，用户名 " + text);
                System.out.println("，密码 " + password);
            }
        });

        addWindowListener(new WindowAdapter() {
        	// 尝试关闭窗体时触发
            public void windowClosing(WindowEvent e) {
                int x = JOptionPane.showConfirmDialog(EventDemo.this, "是否关闭",
                        "窗体关闭确认框",JOptionPane.YES_NO_OPTION);
                if (x == JOptionPane.YES_OPTION) {
                    System.out.println("JFrame.this：确认关闭");
                    dispose();
                } else {
                    System.out.println("JFrame.this：不关闭");
                }
            }
            // 窗口得到焦点时触发
            public void windowActivated(WindowEvent e) {
                System.out.println("JFrame.this: 窗体得到焦点");
            }
            // 窗体失去焦点时触发
            public void windowIconified(WindowEvent e) {
                System.out.println("JFrame.this: 窗体最小化");
            }
        });

        setLayout(new FlowLayout());
        add(textField);
        add(passwordField);
        add(button);
        setLocationRelativeTo(null);
        setVisible(true);
        // 将默认焦点放到button上
        button.requestFocus(); // 需在窗口显示后执行才有效
    }

    public static void main(String[] args) {
        EventDemo demo = new EventDemo();
    }
}